import 'package:dio/dio.dart';
import 'package:flutter_give/http/base_model.dart';
import 'package:oktoast/oktoast.dart';

import '../common_ui/loading.dart';
import '../route/route_utils.dart';

/// 登录体系拦截器
/// 对状态码进行处理 全局错误进行处理
class ResponseInterceptor extends Interceptor {
  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    print("response.statusCode");
    // 这是外层接口成功-并不是后端的状态码
    if (response.statusCode == 200) {
      // 处理接口情况-防止异常
      // var rsp = BaseModel.fromJson(response.data);
      print("===========外层执行成功===========");
      // print(rsp);
      // print(jsonEncode(rsp));
      // print("===========123===========");

      try {
        // 解析数据
        print("===========调用接口===========");
        var rsp = BaseModel.fromJson(response.data);
        // 请求数据成功

        // print("=====666=====");
        print(rsp);
        print(rsp.errorCode);
        print(rsp.code);
        // print("=====666=====");
        if (rsp.errorCode == 0 || rsp.code == 0 || rsp.code == 200) {
          // 没值-返空值
          // 处理请求成功 data为null  把null-改为true   比如收藏接口
          // 根据接口 修改
          print("=================数据向下传递===============");
          if (rsp.data == null || rsp.data == "" || rsp.data == "操作成功") {
            handler.next(
                Response(requestOptions: response.requestOptions, data: true));
            // handler.next(Response(requestOptions: response.requestOptions, data: true));
            // print("======================data没有值====================${response.realUri}");
            // 有值-向下解析
          } else {
            // 传递真实数据
            // handler.next(Response(requestOptions: response.requestOptions, data: rsp.data));
            // 传递数据对象
            handler.next(Response(
                requestOptions: response.requestOptions, data: rsp.data));
            // log("path:${response.realUri}");
            // print("======================向下解析成功====================:${response.realUri}");
          }
        }
        else if (rsp.errorCode == 401 || rsp.code == 30007 || rsp.code == 30012) {
          // 30012 - 长时间未操作
          RouteUtils.navigatorKey.currentState?.pushReplacementNamed('/login');
          handler.reject(DioException(
              requestOptions: response.requestOptions, message: "未登录"));

          showToast("请先登录");
          // 其他情况
        } // token过期
        else if (rsp.errorCode == -1 || rsp.code == 30001 || rsp.code == 30002) {
          if (rsp.data == null) {
            // showToast(rsp.errorMsg ?? "");
            Loading.dismissAll();
            showToast(rsp.msg ?? "");
            handler.next(
                Response(requestOptions: response.requestOptions, data: false));


            if (rsp.msg!.contains("异常")) {
              RouteUtils.navigatorKey.currentState?.pushReplacementNamed('/login');
            }

            // 有值-向下解析
          } else {
            handler.next(Response(
                requestOptions: response.requestOptions, data: rsp.data));
          }
        } // 错误处理
        else {
          showToast("错误状态码==>${rsp.code}");
          handler.reject(DioException(requestOptions: response.requestOptions));
        }
      } catch (e) {
        print("错误执行catch");
        handler.reject(DioException(
            requestOptions: response.requestOptions, message: "$e"));
      }
    } else {
      print("错误执行");
      handler.reject(DioException(requestOptions: response.requestOptions));
    }
  }
}
